package com.wangijun.yuan.web.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangijun.yuan.web.base.CommonPage;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.domain.TagInfo;
import com.wangijun.yuan.web.mapper.TagInfoMapper;
import com.wangijun.yuan.web.model.dto.PersonalTagParam;
import com.wangijun.yuan.web.model.dto.QryTagParam;
import com.wangijun.yuan.web.model.dto.RankingTagQryParam;
import com.wangijun.yuan.web.model.dto.TagListParam;
import com.wangijun.yuan.web.model.vo.TagInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class TagInfoService extends ServiceImpl<TagInfoMapper, TagInfo> {


    private final OperateInfoService operateInfoService;

    /**
     * 功能描述: 标签分页查询
     *
     * @author wlf 2024/7/15 16:25
     */
    public CommonPage<TagInfoVO> tagQryList(TagListParam param) {
        Page<TagInfo> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(param.getText()), TagInfo::getName, param.getText())
                .page(param.page());
        List<Long> followTagIds = operateInfoService.getFollowTagIds();
        return CommonPage.toPage(page, TagInfoVO.class, (vo) -> vo.setFollowFlag(followTagIds.contains(vo.getId())));
    }


    public TagInfoVO get(Long id) {
        if (id == null) {
            CommonResult.error("ID不可为空");
        }
        TagInfo tagInfo = this.getById(id);
        if (tagInfo == null) {
            CommonResult.error("标签信息不存在");
        }
        TagInfoVO tagInfoVO = BeanUtil.copyProperties(tagInfo, TagInfoVO.class);
        List<Long> followTagIds = operateInfoService.getFollowTagIds();
        tagInfoVO.setFollowFlag(followTagIds.contains(tagInfoVO.getId()));
        return tagInfoVO;
    }


    /**
     * 功能描述: 作者用户热度榜单
     *
     * @author wlf 2024/7/21 9:28
     */
    public CommonPage<TagInfoVO> rankingTag(RankingTagQryParam param) {
        LambdaQueryChainWrapper<TagInfo> wrapper = this.lambdaQuery();
        wrapper.orderByDesc(TagInfo::getReadNum);
        Page<TagInfo> page = wrapper.page(param.page());
        List<Long> followTagIds = operateInfoService.getFollowTagIds();
        return CommonPage.toPage(page, TagInfoVO.class, (vo) -> vo.setFollowFlag(followTagIds.contains(vo.getId())));
    }

    public CommonPage<TagInfoVO> followTag(PersonalTagParam param) {
        List<Long> followTagIds = operateInfoService.getFollowTagIds(param.getUserId());
        if (followTagIds.isEmpty()) {
            return CommonPage.toPage(param.page(), TagInfoVO.class);
        }
        Page<TagInfo> page = this.lambdaQuery()
                .in(TagInfo::getId, followTagIds)
                .like(StrUtil.isNotBlank(param.getText()), TagInfo::getName, param.getText())
                .orderByDesc(TagInfo::getReadNum)
                .page(param.page());
        return CommonPage.toPage(page, TagInfoVO.class);
    }

    public CommonPage<TagInfoVO> qryTag(QryTagParam param) {
        Page<TagInfo> page = lambdaQuery()
                .like(StrUtil.isNotBlank(param.getText()), TagInfo::getName, param.getText())
                .orderByDesc(TagInfo::getReadNum)
                .page(param.page());
        List<Long> followTagIds = operateInfoService.getFollowTagIds();
        return CommonPage.toPage(page, TagInfoVO.class, (vo) -> vo.setFollowFlag(followTagIds.contains(vo.getId())));
    }
}
